/**
 * @file yuv5.c
 * @author your name (you@domain.com)
 * @brief yuv 加边框
 * @version 0.1
 * @date 2021-04-28
 * 
 * @copyright Copyright (c) 2021
 * 
 */
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

int simplest_yuv420_border(char *url, int w, int h, int border, int num);

int main(void)
{
    simplest_yuv420_border("../resource/sintel_640_360.yuv", 640, 360, 10, 1);
    return 0;
}

int simplest_yuv420_border(char *url, int w, int h, int border, int num)
{
    FILE *fp = fopen(url, "rb+");
    if (fp == NULL)
    {
        printf("fopen failed[%s] \n", strerror(errno));
        return -1;
    }
    FILE *fp1 = fopen("output_border.yuv", "wb+");
    if (fp1 == NULL)
    {
        printf("fopen failed[%s] \n", strerror(errno));
        return -1;
    }

    uint8_t *pic = (uint8_t *)malloc(w * h * 3 / 2);

    for (int i = 0; i < num; i++) //帧数
    {
        fread(pic, 1, w * h * 3 / 2, fp);

        for (int j = 0; j < h; j++)
        {
            for (int k = 0; k < w; k++)
            {
                if (k < border || k > (w - border) || j < border || j > (h - border))
                {
                    pic[j * w  + k] = 255;//没有想明白，这里为什么要再加个k // 如果是 j *w 则只有350个像素点被修改，如果是 j * w +k 则有 159397个像素点被修改 // j * w 只有 行和列交叉的一个像素点才被赋值
                }
            }
        }

        fwrite(pic, 1, w * h * 3 / 2, fp1);
    }

    free(pic);
    fclose(fp);
    fclose(fp1);

    return 0;
}
